package pe.gob.mininter.dirandro.vaadin.panel.util;

import java.util.List;

import org.apache.commons.lang.StringUtils;

import pe.gob.mininter.dirandro.model.Aerodromo;
import pe.gob.mininter.dirandro.model.Distrito;
import pe.gob.mininter.dirandro.model.Empresa;
import pe.gob.mininter.dirandro.service.AerodromoService;
import pe.gob.mininter.dirandro.service.EmpresaService;
import pe.gob.mininter.dirandro.service.UbigeoService;
import pe.gob.mininter.dirandro.util.Constante;
import pe.gob.mininter.dirandro.vaadin.dialogs.AlertDialog;
import pe.gob.mininter.dirandro.vaadin.util.ComboBoxLOVS;
import pe.gob.mininter.dirandro.vaadin.util.Injector;

import com.vaadin.annotations.AutoGenerated;
import com.vaadin.data.util.BeanItemContainer;
import com.vaadin.ui.Alignment;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Button.ClickListener;
import com.vaadin.ui.ComboBox;
import com.vaadin.ui.CustomComponent;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.TextField;
import com.vaadin.ui.VerticalLayout;

@SuppressWarnings("serial")
public class PanelAgregarAerodromo extends CustomComponent implements ClickListener {

	/*- VaadinEditorProperties={"grid":"RegularGrid,20","showGrid":true,"snapToGrid":true,"snapToObject":true,"movingGuides":false,"snappingDistance":10} */

	@AutoGenerated
	private VerticalLayout mainLayout;
	@AutoGenerated
	private HorizontalLayout pnlPrincipal;
	@AutoGenerated
	private VerticalLayout pnl2;
	@AutoGenerated
	private VerticalLayout pnl2_1;
	@AutoGenerated
	private HorizontalLayout lytBotones;
	@AutoGenerated
	private Button btnEliminar;
	@AutoGenerated
	private Button btnGuardar;
	@AutoGenerated
	private ComboBox cmbUbicacion;
	@AutoGenerated
	private ComboBoxLOVS cmbAeronaveMaxima;
	@AutoGenerated
	private ComboBox cmbEmpresa;
	@AutoGenerated
	private TextField txtNombre;
	private Aerodromo aerodromo;
	private AerodromoService aerodromoService;
	private EmpresaService empresaService;
	private UbigeoService ubigeoService;
	
	public Aerodromo getAerodromo() {
		return aerodromo;
	}
	
	public void setAerodromo(Aerodromo aerodromo) {
		this.aerodromo = aerodromo;
	}
	
	public PanelAgregarAerodromo() {
		buildMainLayout();
		setCompositionRoot(mainLayout);
		aerodromoService=Injector.obtenerServicio(AerodromoService.class);
		empresaService=Injector.obtenerServicio(EmpresaService.class);
		ubigeoService=Injector.obtenerServicio(UbigeoService.class);
		postConstruct();
		debugId();
	}
	private void debugId(){
		
	}
	public void postConstruct() {
	
		txtNombre.setInputPrompt("Nombre");
		txtNombre.setNullRepresentation(StringUtils.EMPTY);
		
		List<Empresa> empresas=empresaService.buscar(null);
		cmbEmpresa.setInputPrompt("Empresa");
		cmbEmpresa.setItemCaptionPropertyId("razonSocial");
		cmbEmpresa.setContainerDataSource(new BeanItemContainer<Empresa>(Empresa.class,  empresas));
		
		cmbAeronaveMaxima.setCodigoLista(Constante.LISTA.CODIGO.TIPO_LETRADO);
		cmbAeronaveMaxima.setInputPrompt("Aeronave Maxima");

		List<Distrito> distritos=ubigeoService.obtenerTodos();
		cmbUbicacion.setInputPrompt("Ubicacion");
		cmbUbicacion.setItemCaptionPropertyId("nombreCompleto");
		cmbUbicacion.setContainerDataSource(new BeanItemContainer<Distrito>(Distrito.class,  distritos));
		
		btnGuardar.addListener(this);
		btnEliminar.addListener(this);
		
	}
	
	@Override
	public void buttonClick(ClickEvent event) {
		if(event.getButton().equals(btnGuardar)){
			aerodromo=new Aerodromo();
			aerodromo.setNombre((String)txtNombre.getValue());
			aerodromo.setEmpresa((Empresa)cmbEmpresa.getValue());
			aerodromo.setAeronaveMaxima(cmbAeronaveMaxima.getValor());
			aerodromo.setUbicacion((Distrito)cmbUbicacion.getValue());
			aerodromoService.crear(aerodromo);
			
			AlertDialog alertDialog = new  AlertDialog("Registro de Aerodromo", "Se ha registrado el aerodromo correctamente", "Aceptar", "400");
			getApplication().getMainWindow().addWindow(alertDialog);
			
			btnGuardar.setEnabled(false);
			detach();
			getApplication().getMainWindow().removeWindow(getWindow());
		}
		if(event.getButton().equals(btnEliminar)) {
			getApplication().getMainWindow().removeWindow(getWindow());
		}
	}
	
	@AutoGenerated
	private VerticalLayout buildMainLayout() {
		// common part: create layout
		mainLayout = new VerticalLayout();
		mainLayout.setImmediate(false);
		mainLayout.setWidth("100%");
		mainLayout.setHeight("-1px");
		mainLayout.setMargin(true);
		mainLayout.setSpacing(true);
		
		// top-level component properties
		setWidth("100.0%");
		setHeight("-1px");
		
		// pnlPrincipal
		pnlPrincipal = buildPnlPrincipal();
		mainLayout.addComponent(pnlPrincipal);
		
		return mainLayout;
	}
	@AutoGenerated
	private HorizontalLayout buildPnlPrincipal() {
		// common part: create layout
		pnlPrincipal = new HorizontalLayout();
		pnlPrincipal.setStyleName("whiteBackGround");
		pnlPrincipal.setImmediate(false);
		pnlPrincipal.setWidth("-1px");
		pnlPrincipal.setHeight("-1px");
		pnlPrincipal.setMargin(true);
		
		// pnl2
		pnl2 = buildPnl2();
		pnlPrincipal.addComponent(pnl2);
		
		return pnlPrincipal;
	}
	@AutoGenerated
	private VerticalLayout buildPnl2() {
		// common part: create layout
		pnl2 = new VerticalLayout();
		pnl2.setImmediate(false);
		pnl2.setWidth("-1px");
		pnl2.setHeight("-1px");
		pnl2.setMargin(false);
		pnl2.setSpacing(true);
		
		// pnl2_1
		pnl2_1 = buildPnl2_1();
		pnl2.addComponent(pnl2_1);
		
		return pnl2;
	}
	@AutoGenerated
	private VerticalLayout buildPnl2_1() {
		// common part: create layout
		pnl2_1 = new VerticalLayout();
		pnl2_1.setImmediate(false);
		pnl2_1.setWidth("-1px");
		pnl2_1.setHeight("-1px");
		pnl2_1.setMargin(false);
		pnl2_1.setSpacing(true);
		
		// txtNombre
		txtNombre = new TextField();
		txtNombre.setCaption("Nombre");
		txtNombre.setImmediate(false);
		txtNombre.setWidth("300px");
		txtNombre.setHeight("-1px");
		pnl2_1.addComponent(txtNombre);
		
		// cmbEmpresa
		cmbEmpresa = new ComboBox();
		cmbEmpresa.setCaption("Empresa");
		cmbEmpresa.setImmediate(false);
		cmbEmpresa.setWidth("300px");
		cmbEmpresa.setHeight("-1px");
		pnl2_1.addComponent(cmbEmpresa);
		
		// cmbAeronaveMaxima
		cmbAeronaveMaxima = new ComboBoxLOVS();
		cmbAeronaveMaxima.setCaption("Aeronave Maxima");
		cmbAeronaveMaxima.setImmediate(false);
		cmbAeronaveMaxima.setWidth("300px");
		cmbAeronaveMaxima.setHeight("-1px");
		pnl2_1.addComponent(cmbAeronaveMaxima);
		
		// cmbUbicacion
		cmbUbicacion = new ComboBox();
		cmbUbicacion.setCaption("Ubicacion");
		cmbUbicacion.setImmediate(false);
		cmbUbicacion.setWidth("300px");
		cmbUbicacion.setHeight("-1px");
		pnl2_1.addComponent(cmbUbicacion);
		
		// lytBotones
		lytBotones = buildLytBotones();
		pnl2_1.addComponent(lytBotones);
		pnl2_1.setComponentAlignment(lytBotones, new Alignment(20));
		
		return pnl2_1;
	}
	@AutoGenerated
	private HorizontalLayout buildLytBotones() {
		// common part: create layout
		lytBotones = new HorizontalLayout();
		lytBotones.setImmediate(false);
		lytBotones.setWidth("-1px");
		lytBotones.setHeight("-1px");
		lytBotones.setMargin(false);
		lytBotones.setSpacing(true);
		
		// btnGuardar
		btnGuardar = new Button();
		btnGuardar.setCaption("Grabar");
		btnGuardar.setImmediate(true);
		btnGuardar.setWidth("-1px");
		btnGuardar.setHeight("-1px");
		lytBotones.addComponent(btnGuardar);
		
		// btnEliminar
		btnEliminar = new Button();
		btnEliminar.setCaption("Cancelar");
		btnEliminar.setImmediate(true);
		btnEliminar.setWidth("-1px");
		btnEliminar.setHeight("-1px");
		lytBotones.addComponent(btnEliminar);
		
		return lytBotones;
	}

}
